package com.hanlink.dsp.ca.model.channel;

import com.google.gson.annotations.Expose;
import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.model.UploadThread;
import com.hanlink.dsp.util.BLog;
import com.hanlink.dsp.util.ByteUtil;
import com.hanlink.dsp.util.Constants;
import com.qztech.bluetooth.BluetoothClientManager;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.io.Serializable;

import static com.hanlink.dsp.DspContext.BLE_WRITE_DELAY;

@Table(name = "_level_channels")
public class LevelChannel implements Serializable {

    @Column(name = "id", isId = true)
    protected int id;

    @Column(name = "channel")
    protected int channel;
    @Column(name = "_check")
    private int checked = 0;

    @Column(name = "settings_id")
    private int settingsId;

    @Column(name = "_val")
    private int val = Constants.MAX_VOL; //
    @Column(name = "_mute")
    private int mute = 0;
    @Column(name = "_degree")
    private int degree = 0;
    @Column(name = "_link")
    private int linked = 0;


    public LevelChannel() {
        super();

    }
    public void resetData(){
        checked = 0;
        val = Constants.MAX_VOL;
        mute = 0;
        degree = 0;
        linked = 0;
    }



    public LevelChannel(int channel) {
        this.channel = channel;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public int getSettingsId() {
        return settingsId;
    }

    public void setSettingsId(int settingsId) {
        this.settingsId = settingsId;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        if (val > Constants.MAX_VOL) val = Constants.MAX_VOL;
        this.val = val;
    }

    public int getMute() {
        return mute;
    }

    public void setMute(int mute) {
        this.mute = mute;
    }

    public int getChecked() {
        return checked;
    }

    public boolean isChecked() {
        return checked == 1;
    }

    public void setChecked(int checked) {
        this.checked = checked;
    }

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }

    public void setLinked(int linked) {
        this.linked = linked;
    }

    public int getLinked() {
        return linked;
    }

    @Override
    public String toString() {
        return "LevelChannel{" +
                "id=" + id +
                ", channel=" + channel +
                ", checked=" + checked +
                ", settingsId=" + settingsId +
                ", val=" + val +
                ", mute=" + mute +
                ", degree=" + degree +
                ", linked=" + linked +
                '}';
    }

    @Expose(serialize = false, deserialize = false)
    private byte[] buffer = new byte[]{(byte) 0xAA,(byte) 0x55, (byte) 0xDB, (byte) 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    public byte[] getCmdData() {
        buffer[3] = (byte) (0xC1 + channel);
        if (mute == 1) {
            buffer[4] = (byte) 0x01;
        } else {
            buffer[4] = (byte) 0x00;
        }
        int vol = Math.abs(val - Constants.MAX_VOL)* 10;
        buffer[5] = (byte) (vol >> 8 & 0xff);
        buffer[6] = (byte) (vol & 0xff);
//        if (degree == 1) {
//            buffer[7] = (byte) 0x01;
//        } else {
//            buffer[7] = (byte) 0x00;
//        }
        return buffer;
    }

    public void sendVolume(){
        byte[] buffer = getVolumeBuffer();
        BLog.d("volume", ByteUtil.bytesToHexString(buffer));
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);
    }

    public byte[] getVolumeBuffer(){
        byte[] buffer = new byte[]{(byte) 0xAA,(byte) 0x55, (byte) 0xDB, (byte) 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        buffer[3] = (byte) (0xC1 + channel);
        if (mute == 1) {
            buffer[4] = (byte) 0x01;
        }
        int vol = Math.abs(val - Constants.MAX_VOL) * 10;
        buffer[5] = (byte) (vol >> 8 & 0xff);
        buffer[6] = (byte) (vol & 0xff);
        return buffer;
    }

    public void sendDegree(){
       buffer = getDegreeBuffer();
        BLog.d("degree", ByteUtil.bytesToHexString(buffer));
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);
    }

    public byte[] getDegreeBuffer(){
        byte[] buffer = new byte[]{(byte) 0xAA,(byte) 0x55, (byte) 0xBB, (byte) 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        buffer[3] = (byte) (0xC1 + channel);
        if (degree == 1) {
            buffer[4] = (byte) 0x01;
        }
        return buffer;
    }

    public void sendCmd() {
       sendVolume();
       sendDegree();
    }

    public void sendCmdUpload() {
        sendVolume();
        try {
            Thread.sleep(BLE_WRITE_DELAY);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sendDegree();
    }

    public LevelChannel copy() {
        final LevelChannel lc = new LevelChannel();
        lc.id = id;
        lc.channel = channel;
        lc.checked = checked;
        lc.settingsId = settingsId;
        lc.val = val;
        lc.mute = mute;
        lc.degree = degree;
        lc.linked = linked;
        System.arraycopy(buffer,0, lc.buffer,0, buffer.length);
        return lc;
    }
}
